package cn.jupiter.db;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.jupiter.json.RestResource;

public class Map2DataBase {
	protected final Log log = LogFactory.getLog(Map2DataBase.class);
	private int code=0;
	//�������
	public void insertJson(Connection conn,RestResource restResource){

		String sql=getCreateSql(restResource);
		log.debug("create sql:"+sql);
		Statement stmt;
		try {
			conn.setAutoCommit(false);
			stmt = conn.createStatement();
			boolean b=stmt.execute(sql, code);
			conn.commit();
		
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(!conn.isClosed()){
					conn.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}	
	}
	//��ϸ���
	public void updateJson(Connection conn,RestResource restResource){
		String sql=this.getUpdateSql(restResource);
		log.debug("update Json sql:"+sql);
		Statement stmt;
		try {
			stmt = conn.createStatement();
			ResultSet resultSet=stmt.executeQuery(sql);
			conn.commit();
			resultSet.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(!conn.isClosed()){
					conn.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}	
	}
	//�������
	public void deleteJson(Connection conn,RestResource restResource){
		String sql=this.getDeleteSQL(restResource);
		log.debug("deleteJson sql:"+sql);
		Statement stmt;
		try {
			conn.setAutoCommit(false);
			stmt = conn.createStatement();
			
			stmt.execute(sql);
			conn.commit();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(!conn.isClosed()){
					conn.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}	
	}

	/**
	 * ���jsonת����map���������sql
	 * @param bean
	 * @return
	 */
	private String getCreateSql(RestResource restResource){
		//����ύ��resouce�������
		Map<String,Object> beans=restResource.getRow();
		String tableName=restResource.getResouce();
		
		String sql=("insert into "+ restResource.getResouce()+" (");
		
		//oracle��ݿ�ʹ��sequence4�������
		if(DbUtils.DATABASE_DIALECT.equals(DbUtils.ORACLE_DIALECT)){
			sql="insert into "+tableName+" (id,";
		}
		//mysql����Ҫ��ݿ�����Ϊ����
		if(DbUtils.DATABASE_DIALECT.equals(DbUtils.MYSQL_DIALECT)){
			sql="insert into "+tableName+" (";
		}
		for (Object s:beans.keySet()){
			if(!"id".endsWith(s.toString())){
				sql+=s+",";
			}
			
		}
		sql=sql.substring(0,sql.length()-1);
		//oracle��ݿ�ʹ��sequence4�������
		if(DbUtils.DATABASE_DIALECT.equals(DbUtils.ORACLE_DIALECT)){
			sql+=" ) values("+DbUtils.ORACLE_SEQUENCE+".NEXTVAL,";
		}
		//mysql����Ҫ��ݿ�����Ϊ����
		if(DbUtils.DATABASE_DIALECT.equals(DbUtils.MYSQL_DIALECT)){
			sql+=" ) values(";
		}
		
		for (Object s:beans.keySet()){
			if(!"id".endsWith(s.toString())){
				if(java.lang.Integer.class.equals(beans.get(s).getClass())){
					sql+=beans.get(s)+",";
				}
				else{
					sql+="'"+beans.get(s)+"',";
				}			
			}
		}
		sql=sql.substring(0,sql.length()-1);
		sql+=")";
		return sql;
	}
	/**
	 * ����bean��sql
	 * @param bean
	 * @return
	 */
	private String getUpdateSql(RestResource restResource){
		//����ύ��resouce�������
		Map<String,Object> beans=restResource.getRow();
		String tableName=restResource.getResouce();
		String sql="update "+tableName+" set ";
		for (Object s:beans.keySet()){
			if(!"id".endsWith(s.toString())){
			if(java.lang.Integer.class.equals(beans.get(s).getClass())||java.lang.Double.class.equals(beans.get(s).getClass())){
				sql+=s+"=";
				sql+=beans.get(s)+",";
			}
			else{
				sql+=s+"='";
				sql+=beans.get(s)+"',";
			}
			}
		}
		sql=sql.substring(0,sql.length()-1);
		sql+=" where id="+beans.get("id");
		return sql;
	}
	
	private String getDeleteSQL(RestResource restResource){
		//����ύ��resouce�������
		
		String tableName=restResource.getResouce();
		String sql="delete from "+tableName+" where id="+restResource.getId();
		return sql;
	}
	public int getCode() {
		return code;
	}
	public void setCode(int code) {
		this.code = code;
	}
	
}
